package com.client.service.impl;

import com.client.pojo.Entries;
import com.client.dao.EntriesDao;
import com.client.service.EntriesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 知识百科词条表 服务实现类
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 08:28:54
 */
@Service
public class EntriesServiceImpl extends ServiceImpl<EntriesDao, Entries> implements EntriesService {
    @Autowired
    private EntriesDao entriesDao;

    @Override
    public Entries getEntryByIdWithUser(Integer entryId) {
        return entriesDao.selectByIdWithUser(entryId);
    }

    @Override
    public java.util.List<Entries> getEntriesListWithUserAvatar(QueryWrapper<Entries> queryWrapper) {
        return entriesDao.selectListWithUserAvatar(queryWrapper);
    }

    @Override
    public IPage<Entries> getEntriesPageWithUserAvatar(Integer page, Integer limit) {
        // 创建分页对象
        Page<Entries> pageWrapper = new Page<>(page, limit);
        
        // 创建查询条件（按entry_id降序排序，确保不同数据均匀分布）
        QueryWrapper<Entries> queryWrapper = new QueryWrapper<>();
        
        
        // 调用DAO层方法执行分页查询
        IPage<Entries> result = entriesDao.selectPageWithUserAvatar(pageWrapper, queryWrapper);
        
        // 打印日志用于调试
        System.out.println("查询结果总数: " + result.getTotal());
        System.out.println("查询结果记录数: " + result.getRecords().size());
        
        if (result.getRecords().size() > 0) {
            System.out.println("第一条记录entryId: " + result.getRecords().get(0).getEntryId());
            System.out.println("第一条记录entryName: " + result.getRecords().get(0).getEntryName());
            System.out.println("第一条记录publisherId: " + result.getRecords().get(0).getPublisherId());
            if (result.getRecords().get(0).getPublisher() != null) {
                System.out.println("第一条记录publisher username: " + result.getRecords().get(0).getPublisher().getUsername());
            }
        }
        
        return result;
    }

    @Override
    public IPage<Entries> getHot(Integer page, Integer limit) {
         // 创建分页对象
        Page<Entries> pageWrapper = new Page<>(page, limit);
        QueryWrapper<Entries> queryWrapper = new QueryWrapper<>();
        // 调用DAO层方法执行分页查询
        IPage<Entries> result = entriesDao.getHot(pageWrapper, queryWrapper);
        return result;
    }   
    @Override
    public void addViews(Integer entryId) {
        entriesDao.addViews(entryId);
    }
    @Override
    public void addLikes(Integer entryId) {
        entriesDao.addLikes(entryId);
    }
    @Override
    public void addCollects(Integer entryId) {
        entriesDao.addCollects(entryId);
    }
    @Override
    public void cancelLikes(Integer entryId) {
        entriesDao.cancelLikes(entryId);
    }
    @Override
    public void cancelCollects(Integer entryId) {
        entriesDao.cancelCollects(entryId);
    }
    @Override
    public List<Entries> searchEntry(String keyword, Integer offset, Integer limit) {
   
        return entriesDao.searchEntry(keyword, offset, limit);
    }
    @Override
    public List<Entries> searchFirstCategory(Integer categoryId, Integer offset, Integer limit) {
        return entriesDao.searchFirstCategory(categoryId, offset, limit);
    }
}
